prepare("INSERT INTO exams (name, year, subject_id, duration, status) VALUES (?, ?, ?, ?, ?)"); $stmt->execute([$name, $year, $subject_id, $duration, $status]); $success = "Exam created successfully!"; } catch (PDOException $e) { $error = "Error creating exam: " . $e->getMessage(); } } // Handle exam update if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['update_exam'])) { $exam_id = sanitize_input($_POST['exam_id']); $name = sanitize_input($_POST['name']); $year = sanitize_input($_POST['year']); $subject_id = sanitize_input($_POST['subject_id']); $duration = sanitize_input($_POST['duration']); $status = sanitize_input($_POST['status']); try { $stmt = $pdo->prepare("UPDATE exams SET name = ?, year = ?, subject_id = ?, duration = ?, status = ? WHERE id = ?"); $stmt->execute([$name, $year, $subject_id, $duration, $status, $exam_id]); $success = "Exam updated successfully!"; } catch (PDOException $e) { $error = "Error updating exam: " . $e->getMessage(); } } // Handle exam deletion if (isset($_GET['delete_id'])) { $delete_id = sanitize_input($_GET['delete_id']); try { // Check if exam has questions $stmt = $pdo->prepare("SELECT COUNT(*) as question_count FROM questions WHERE exam_id = ?"); $stmt->execute([$delete_id]); $result = $stmt->fetch(PDO::FETCH_ASSOC); if ($result['question_count'] > 0) { $error = "Cannot delete exam that has questions. Please delete the questions first."; } else { $stmt = $pdo->prepare("DELETE FROM exams WHERE id = ?"); $stmt->execute([$delete_id]); $success = "Exam deleted successfully!"; } } catch (PDOException $e) { $error = "Error deleting exam: " . $e->getMessage(); } } // Get all exams with subject names $stmt = $pdo->prepare(" SELECT e.*, s.name as subject_name, (SELECT COUNT(*) FROM questions WHERE exam_id = e.id) as question_count, (SELECT COUNT(*) FROM exam_sessions WHERE exam_id = e.id) as attempt_count FROM exams e LEFT JOIN subjects s ON e.subject_id = s.id ORDER BY e.year DESC, e.name "); $stmt->execute(); $exams = $stmt->fetchAll(PDO::FETCH_ASSOC); // Get subjects for dropdown $stmt = $pdo->prepare("SELECT * FROM subjects ORDER BY name"); $stmt->execute(); $subjects = $stmt->fetchAll(PDO::FETCH_ASSOC); // Get exam for editing $edit_exam = null; if (isset($_GET['edit_id'])) { $edit_id = sanitize_input($_GET['edit_id']); $stmt = $pdo->prepare("SELECT * FROM exams WHERE id = ?"); $stmt->execute([$edit_id]); $edit_exam = $stmt->fetch(PDO::FETCH_ASSOC); } ?> Manage Exams - Admin

Manage Exams

Create, edit, and manage examination schedules

Add|Manage Subjects

❌ Cancel

All Exams ()

0): ?>

📖 Subject:
⏱️ Duration: minutes
Questions:
👥 Attempts:
📅 Created:
📚

No Exams Created Yet

Create your first exam to get started with the CBT system.